<!-- 定额管理 -->

<template>
    <div class="leftRightContent">
        <!-- 左边子系统，
        右边菜单 -->
        <div class="leftContent" style="width: 250px; padding: 10px">
            <el-input placeholder="输入关键字搜索" v-model="deviceName" class="input-with-select">
                <el-button slot="append" icon="el-icon-search" @click="searchDevice"></el-button>
            </el-input>
            <div class="ddManager">
                <buildTree
                    :checkStrictly="false"
                    :showCheck="true"
                    :key="buildKey"
                    :height="secondTableHeight"
                    @handleCurrentChange="handleCurrentChange"
                    @load="loadBuild"
                    @onCheck="onCheck"
                ></buildTree>
            </div>
        </div>
        <div class="rightContent" style="width: calc(100% - 260px); padding: 10px">
            <quotaManagementList :treeNode="checkList"></quotaManagementList>
        </div>
    </div>
</template>

<script>
import customDialog from '@/components/customComponents/customDialog'
import SelectUser from '@/components/customComponents/managementCenter/SelectUser'
import buildTree from '@/components/customComponents/managementCenter/buildTree'
import customButton from '@/components/customComponents/customForm/customButton'
import formIndex from '@/components/customComponents/customForm/formIndex'
import tableIndex from '@/components/customComponents/customeTable/iviewTable/singleViewTableAsync'
import navigationBar from '@/components/customComponents/managementCenter/navigationBarCustomTitle'
import {AjaxCommon} from '@/libs/apiCommon'
import {CardId} from '@/libs/cardId'
import {PopupCommon} from '@/libs/popupCommon'
import {Utility} from '@/libs/utility'
import quotaManagementList from './quotaManagementList'
export default {
    components: {
        navigationBar,
        customButton,
        tableIndex,
        customDialog,
        formIndex,
        SelectUser,
        buildTree,
        quotaManagementList
    },
    data() {
        return {
            deviceName: '',
            sbType: 'base',
            buildKey: 'test1',
            buildInfo: {},
            level: 1,
            allMenu: [],
            menuItem: {},
            optionValue: 'name',
            options: [
                {
                    value: 'name',
                    label: '登录名'
                },
                {
                    value: 'realname',
                    label: '真实姓名'
                }
            ],
            sysInfo: {},
            secondTableHeight: document.body.clientHeight - 190,
            selectOrgList: [],
            userName: null,
            status: '',
            curTitle: '菜单管理',
            statusList: Utility.getDictList('status'),
            // table 相关参数  begin
            dataSource: [],
            tableColumns: [
                {
                    type: 'index',
                    width: 60,
                    align: 'center'
                },
                {
                    key: 'name',
                    title: '菜单名称',
                    tree: true
                },
                {
                    key: 'menuIco',
                    title: '图标'
                },
                {
                    key: 'order_no',
                    title: '排序'
                    // slot: 'sex'
                },
                {
                    key: 'url',
                    title: '路由路径'
                },
                {
                    title: '操作',
                    slot: 'action',
                    width: 140,
                    align: 'center'
                }
            ],
            btnList: ['add', 'edit', 'delete'],
            // pageSize: 20,
            pageSize: 1000,
            pageCount: 100,
            pageNo: 1,
            // table 相关参数  end

            // 弹窗是否显示
            dialogShow: false,
            // 弹窗标题 (新增修改不同)
            dialogTitle: '',

            type: 'add',
            userList: [],
            userInfo: {},
            // 是否是所有数据了
            isAll: false,
            checkList: []
        }
    },
    created() {},
    methods: {
        onCheck(list) {
            this.checkList = list
        },
        loadBuild() {
            setTimeout(() => {
                try {
                    document
                        .getElementsByClassName('ddManager')[0]
                        .children[0].children[0].children[0].children[0].children[0].children[0].click()

                    document
                        .getElementsByClassName('ddManager')[0]
                        .children[0].children[0].children[0].children[0].children[0].children[1].click()
                } catch (error) {}
            }, 100)
        },
        searchDevice() {
            const deviceName = this.deviceName
            console.log(deviceName)
            // this.deviceitem.eq_name = deviceName
            // this.GetDateDivce(this.deviceitem.build_id, this.deviceitem.eq_type, this.deviceitem.eq_name, this.deviceitem.typeid, 1)
        },
        success(success) {
            if (success) {
                this.buildKey = `test${parseInt(Math.random() * 100000000)}`
            }
        },
        getData(d) {
            const self = this
            var params = {
                sysGuid: this.sysInfo.id,
                menuName: d.menuName,
                mobileName: '',
                parentId: d.parentId,
                menuUrl: d.menuUrl,
                menuIco: d.menuIco,
                mobileUrl: '',
                ismobile: 0,
                menuSort: d.menuSort
            }
            let apiAddress = `/api/Menu/AddMenu`
            let oprText = '新增'
            if (d.id) {
                params.menuId = d.id
                apiAddress = `/api/Menu/EditMenu`
                oprText = '编辑'
            }
            AjaxCommon(apiAddress, 'post', params).then((res) => {
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', oprText + '成功')
                    self.dialogShow = false
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        handleCurrentChange(d) {
            const self = this
            AjaxCommon(`/api/Build/GetBuildInfo?bd_guid=${d.id}`, 'get', null).then((res) => {
                self.buildInfo = res?.data?.Message?.[0] ?? {}
            })
        },
        getUserData(uList) {
            let self = this
            let tempData = {}
            tempData.ROLE_NAME = this.userInfo.ROLE_NAME
            tempData.USERS_USERID = uList.map((t) => t.user_id).toString()
            AjaxCommon('/api/Role/EditRoleUser', 'post', tempData).then((res) => {
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', '编辑成功')
                    self.initPage()
                    self.dialogSelectUserShow = false
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        getHeight() {
            this.secondTableHeight = document.body.clientHeight - 190
        },
        customClick(isTrue) {
            this.useOrg(isTrue)
        },
        handleSelectionChange(d) {
            this.selectOrgList = d
        },
        relationUserRow(d) {
            this.userInfo = d
            let self = this
            // 根据角色名获取角色下所有用户
            AjaxCommon(`/api/role/GetRoleUsers/${d.ROLE_NAME}/10000/1`, 'get', null).then((res) => {
                let userList = []
                if (res?.data?.Code == 200) {
                    userList = res.data.Message.rows
                }
                self.userList = userList
                self.dialogSelectUserShow = true
            })
        },
        formInputChange(e) {
            if (e.filedName == 'idCard') {
                const data = CardId.getInfoByCardId(e.value)
                if (data) {
                    let newData = e.allData
                    newData['birthday'] = data.birthday
                    newData['sex'] = data.sex
                    this.$refs.form.changeFormValue(newData)
                }
            }
        },
        dialogClose() {
            this.dialogShow = false
        },
        addClick() {
            if (this.sysInfo.id) {
                this.type = 'add'
                const formValidate = {
                    parentId: '',
                    menuIco: '',
                    menuName: '',
                    menuUrl: '',
                    menuSort: 1,
                    id: ''
                }
                this.menuItem = formValidate
                this.dialogTitle = '新增'
                this.dialogShow = true
            } else {
                PopupCommon.notify('info', '请先选择左侧的子系统')
            }
        },
        // 分页点击事件
        pageChange(p) {
            this.pageNo = p
            this.initPage()
        },
        addRow(d) {
            this.type = 'add'
            const formValidate = {
                parentId: d.id,
                menuIco: '',
                menuName: '',
                menuUrl: '',
                menuSort: 1,
                id: ''
            }
            this.menuItem = formValidate
            this.dialogTitle = '新增'
            // // 调用详情接口
            // AjaxCommon(`/api/Menu/GetDataById?menuid=${d.id}`, 'get', null)
            this.dialogShow = true
        },
        editRow(d) {
            this.type = 'edit'
            const formValidate = {
                parentId: d.parentid,
                menuIco: d.menuIco,
                menuName: d.name,
                menuUrl: d.url,
                menuSort: d.order_no,
                id: d.id
            }
            this.menuItem = formValidate
            this.dialogTitle = '编辑'
            // // 调用详情接口
            // AjaxCommon(`/api/Menu/GetDataById?menuid=${d.id}`, 'get', null)
            this.dialogShow = true
        },
        deleteRow(d) {
            let self = this
            AjaxCommon(`/api/Menu/DeleteMenu?sysGuid=${d.id}`, 'post', {}).then((res) => {
                if (res?.data?.Code == 200) {
                    PopupCommon.notify('success', '删除成功')
                    self.initPage()
                } else {
                    const msg = res?.data?.Message
                    if (msg) {
                        PopupCommon.notify('error', msg)
                    }
                }
            })
        },
        // 初始化事件
        async initPage() {
            let self = this
            // 获取所有菜单
            const allSysData = await AjaxCommon(`/api/Menu/GetSysMenuData`, 'get', null)
            const listData = allSysData?.data?.Message ?? []
            const sysData = listData?.find((t) => t.id == this.sysInfo.id)
            // id   text
            const sysMenuData = sysData?.children
            this.allMenu = sysMenuData ?? []
            const params = `/api/Menu/SysMenuTreeData?sysGuid=${this.sysInfo.id}&level=${this.level}&PageSize=${this.pageSize}&PageIndex=${this.pageNo}`
            AjaxCommon(params, 'get', null).then((res) => {
                if (res?.data?.Code == 200) {
                    const data = res?.data.Message
                    data?.map((t) => {
                        // t.hasChildren = t.state == 'closed'
                        if (t.state == 'closed') {
                            t._loading = false
                            t.children = []
                        }
                        //   _loading: false,
                        // children: []
                        return t
                    })
                    self.dataSource = data
                } else {
                    PopupCommon.notify('error', res?.data?.msg)
                }
            })
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.getHeight()
        })

        /*窗口自适应，关键代码*/
        window.onresize = () => {
            this.getHeight()
        }
    }
}
</script>

<style></style>
